home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / uuconf.subproj / addstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  3.9 KB  |  140 lines

  1. /* addstr.c
  2.    Add a string to a list of strings.
  3.  
  4.    Copyright (C) 1992 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP uuconf library.
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License
  10.    as published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucnfi.h"
  27.  
  28. #if USE_RCS_ID
  29. const char _uuconf_addstr_rcsid[] = "$Id: addstr.c,v 1.7 1995/06/21 19:21:26 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33.  
  34. /* When setting system information, we need to be able to distinguish
  35.    between a value that is not set and a value that has been set to
  36.    NULL.  We do this by initializing the value to point to the
  37.    variable _uuconf_unset, and then correcting it in the function
  38.    _uuconf_isystem_basic_default.  This variable is declared in this
  39.    file because some linkers will apparently not pull in an object
  40.    file which merely declarates a variable.  This functions happens to
  41.    be pulled in by almost everything.  */
  42.  
  43. char *_uuconf_unset;
  44.  
  45. /* Add a string to a list of strings.  The list is maintained as an
  46.    array of elements ending in NULL.  The total number of available
  47.    slots is always a multiple of CSLOTS, so by counting the current
  48.    number of elements we can tell whether a new slot is needed.  If
  49.    the fcopy argument is TRUE, the new string is duplicated into
  50.    memory.  If the fcheck argument is TRUE, this does not add a string
  51.    that is already in the list.  The pblock argument may be used to do
  52.    the allocations within a memory block.  This returns a standard
  53.    uuconf error code.  */
  54.  
  55. #define CSLOTS (8)
  56.  
  57. int
  58. _uuconf_iadd_string (qglobal, zadd, fcopy, fcheck, ppzstrings, pblock)
  59.      struct sglobal *qglobal;
  60.      char *zadd;
  61.      boolean fcopy;
  62.      boolean fcheck;
  63.      char ***ppzstrings;
  64.      pointer pblock;
  65. {
  66.   char **pz;
  67.   size_t c;
  68.  
  69.   if (fcheck && *ppzstrings != NULL)
  70.     {
  71.       for (pz = *ppzstrings; *pz != NULL; pz++)
  72.     if (strcmp (zadd, *pz) == 0)
  73.       return UUCONF_SUCCESS;
  74.     }
  75.  
  76.   if (fcopy)
  77.     {
  78.       size_t clen;
  79.       char *znew;
  80.  
  81.       clen = strlen (zadd) + 1;
  82.       znew = (char *) uuconf_malloc (pblock, clen);
  83.       if (znew == NULL)
  84.     {
  85.       if (qglobal != NULL)
  86.         qglobal->ierrno = errno;
  87.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  88.     }
  89.       memcpy ((pointer) znew, (pointer) zadd, clen);
  90.       zadd = znew;
  91.     }
  92.  
  93.   pz = *ppzstrings;
  94.   if (pz == NULL || pz == (char **) &_uuconf_unset)
  95.     {
  96.       pz = (char **) uuconf_malloc (pblock, CSLOTS * sizeof (char *));
  97.       if (pz == NULL)
  98.     {
  99.       if (qglobal != NULL)
  100.         qglobal->ierrno = errno;
  101.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  102.     }
  103.       *ppzstrings = pz;
  104.     }
  105.   else
  106.     {
  107.       c = 0;
  108.       while (*pz != NULL)
  109.     {
  110.       ++pz;
  111.       ++c;
  112.     }
  113.  
  114.       if ((c + 1) % CSLOTS == 0)
  115.     {
  116.       char **pznew;
  117.  
  118.       pznew = (char **) uuconf_malloc (pblock,
  119.                        ((c + 1 + CSLOTS)
  120.                         * sizeof (char *)));
  121.       if (pznew == NULL)
  122.         {
  123.           if (qglobal != NULL)
  124.         qglobal->ierrno = errno;
  125.           return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  126.         }
  127.       memcpy ((pointer) pznew, (pointer) *ppzstrings,
  128.           c * sizeof (char *));
  129.       uuconf_free (pblock, *ppzstrings);
  130.       *ppzstrings = pznew;
  131.       pz = pznew + c;
  132.     }
  133.     }
  134.  
  135.   pz[0] = zadd;
  136.   pz[1] = NULL;
  137.  
  138.   return UUCONF_SUCCESS;
  139. }
  140.